// config  基础配置
const express = require("express");
const path = require('path');
const app = express();
const fs = require("fs");

app.use(express.urlencoded({
	extended: false
}));
app.use(express.json());
const port = 8081; //端口
global.isProd = 1//1线上0测试
global.port=port;
global.maxFileSize = 450 * 1024 * 1024; //视频文件大小 b kb mb
global.maxImageFileSize = 80 * 1024 * 1024; //图片文件大小
global.agreement = isProd ? "https" : "http"
global.rootDirname = __dirname;
/*文件实体过大问题*/
// app.use(bodyparser.json({limit:'500mb'}));
// app.use(bodyparser.urlencoded({limit:'500mb',extended:true}));
// 设置CORS的中间件
const cors = require('cors');
const shsAllowedOrigins = ['http://ctrlikun.com', 'https://ctrlikun.com',
	'https://www.ctrlikun.com', 'http://www.ctrlikun.com',
	'http://www.ctrlikun.com/', 'http://ctrlikun.com/',
	"shsim.ctrlikun.com", "http://shsim.ctrlikun.com", "https://shsim.ctrlikun.com"
];
shsAllowedOrigins.push("http://127.0.0.1:8080", 'http://localhost:8080')
// shsAllowedOrigins.push("http://192.168.31.78:8080")
app.use(cors({
	origin: function(origin, callback) {
		// 允许预检请求
		if (!origin || shsAllowedOrigins.indexOf(origin) !== -1) {
			callback(null, true);
		} else {
			callback(new Error('SHS--Not allowed by CORS'));
		}
	}, // 允许的源
	methods: ['GET', 'POST', 'PUT', 'DELETE'], // 允许的HTTP请求方法
	allowedHeaders: ['Content-Type', 'Authorization'], // 允许的HTTP请求头
	optionsSuccessStatus: 204 // 成功的HTTP响应状态码
}));


app.use(express.json({ limit: '500mb' }));
app.use(express.urlencoded({ limit: '500mb', extended: true }));

const getFileImage = require("./utils/getFile.js")
//获取图片 压缩版
app.get("/imgs/*", getFileImage);
//获取图片 无压缩版
//静态资源管理  无压缩版
app.use(express.static(path.join(__dirname, 'public')))



const upload_file_video = require("./root/uploadVideo.js")
const upload_file_image = require("./root/uploadimage.js")
const upload_file_video_hls = require("./root/uploadVideoHls.js")


//引入文件过滤中间件
const {
	upload_iamge,
	upload_video
} = require("./utils/fileUpload.js")

/*上传图片*/
app.post("/upload_file_image", upload_iamge.single("shsuploadfile"), upload_file_image);
/*上传视频*/
app.post("/upload_file_video", upload_video.single("shsuploadfile"), upload_file_video);
/**上传视频hls*/
app.post("/upload_file_video_hls", upload_video.single("shsuploadfile"), upload_file_video_hls);

app.listen(port, () => {
	console.log(`images server start http://127.0.0.1:${port}`);
})